Just as there are many properties that most objects share, they also have many methods in common. In this section, we examine these methods.
If a control supports Left, Top, Width, and Height properties, it also supports the Move method, through which you can change some or all four properties in a single operation. The following example changes three properties: Left, Top, and Width.
' Double a form's width, and move it to the upper left corner of the screen. ' Syntax is: Move Left, Top, Width, Height. Form1.Move 0, 0, Form1.Width * 2 |
Note that all arguments but the first one are optional, but you can't omit any of them in the middle of the command. For example, you can't pass the Height argument if you omit the Width argument. As I mentioned in the description of individual properties, you should be aware that the Height property is read-only for the ComboBox control.
TIP
The Move method should always be preferred to individual property assignment for at least two reasons: This operation is two to three times faster than four distinct assignments, and if you're modifying the Width and Height properties of a form, each individual property assignments would fire a separate Resize event, thus adding a lot of overhead to your code.
The Refresh method causes the control to be redrawn. You normally don't need to explicitly call this method because Visual Basic automatically refreshes the control's appearance when it has a chance (usually when no user code is running and Visual Basic is in an idle state). But you can explicitly invoke this method when you modify a control's property and you want the user interface to be immediately updated:
For n = 1000 To 1 Step -1 Label1.Caption = CStr(i) Label1.Refresh ' Update the label immediately. Next |
CAUTION
You can also refresh a form using the DoEvents command because it yields the control to Visual Basic, and the Visual Basic form engine exploits this opportunity to update the user interface. But you should be aware that DoEvents performs additional processing as well—for example, it checks whether any button has been clicked and if so it executes its Click procedure. Therefore, the two techniques aren't always equivalent. In general, using the Refresh method on the only control that has been modified delivers better performance than executing a DoEvents command. It also avoids reentrancy problems that can occur, for example, when the user clicks again on the same button before the previous Click procedure has completed its processing. If you want to update all the controls on a form but you don't want the end user to interact with the program, just execute the Refresh method of the parent form.
The SetFocus method moves the input focus on the specified control. You need to call this method only if you want to modify the default Tab order sequence that you implicitly create at design time by setting the TabIndex property of the controls on the form, as we saw in Chapter 1. The control whose TabIndex property is set to 0 receives the focus when the form loads.
A potential problem with the SetFocus method is that it fails and raises a run-time error if the control is currently disabled or invisible. For this reason, avoid using this method in the Form_Load event (when all controls aren't yet visible) and you should either ensure that the control is ready to receive the focus or protect the method with an On Error statement. Here's the code for the former approach:
' Move the focus to Text1. If Text1.Visible And Text1.Enabled Then Text1.SetFocus End If |
And here's the code for the other possible approach, using the On Error statement:
' Move the focus to Text1. On Error Resume Next Text1.SetFocus |
TIP
The SetFocus method is often used in the Form_Load event procedure to programmatically set which control on the form should receive the focus when the form initially appears. Because you can't use SetFocus on invisible controls, you're forced to make the form visible first:
Private Sub Form_Load() Show ' Make the form visible. Text1.SetFocus End SubHere's another possible solution:
Private Sub Form_Load() Text1.TabIndex = 0 End SubNote that if Text1 isn't able to receive the input focus (for example, its TabStop property is set to False), Visual Basic automatically moves the focus on the next control in the Tab order sequence, without raising any error. The drawback of this second approach is that it affects the Tab order of all other controls on the form.
The ZOrder method affects the visibility of the control with respect to other overlapping controls. You just execute this method without any argument if you want to position the control in front of other controls; or you can pass 1 as an argument to move the control behind other controls:
' Move a control behind any other control on the form. Text1.ZOrder 1 Text1.ZOrder ' Move it in front. |
Note that you can set the relative z-order of controls at design time using the commands in the Order submenu of the Format menu, and you can also use the Ctrl+J key combination to bring the selected control to the front or the Ctrl+K key combination to move it behind other controls.
The actual behavior of the ZOrder method depends on whether the control is standard or lightweight. In fact, lightweight controls can never appear in front of standard controls. In other words, the two types of controls—standard and lightweight—are located on distinct z-order layers, with the layer of standard controls in front of the layer of lightweight controls. This means that the ZOrder method can change the relative z-order of a control only within the layer it belongs to. For example, you can't place a Label (lightweight) control in front of a TextBox (standard) control. However, if the standard control can behave like a container control—a PictureBox or a Frame control, for example—you can make a lightweight control appear in front of the standard control if you place the lightweight control inside that container control, as you can see in Figure 2-5.
The ZOrder method also applies to forms. You can send a form behind all other forms in the same Visual Basic application, or you can bring it in front of them. You can't use this method, however, to control the relative position of your forms with respect to windows belonging to other applications.
Figure 2-5. Relative z-order of controls.